import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/http/apis/session/session_model.dart';
import 'package:xim/common/util/collection.dart';
import 'package:xim/screen/message_list_screen/chat_list_style.dart';
import 'chat_list_controller.dart';

/// 消息列表页面
class ChatListScreen extends GetStyleControllerView<List<SessionMessageModel>, ChatListController, ChatListStyle> {

  final RefreshController _refreshController = RefreshController(initialRefresh: false);

  ChatListScreen({super.key});

  /// 会话信息
  List<Widget> get chatViews => controller
      .sessionList
      .map((e) => GestureDetector(
          child: messageRowItem(e, edgeInsets: style.messageEdgeInsets),
          onTap: () => Routers.toChat(e.id)))
      .toList();

  @override
  Widget buildView(BuildContext context, data) {
    return Scaffold(
      appBar: style.appBar,
      body: SmartRefresher(
        controller: _refreshController,
        onLoading: () async {
          await Future.delayed(const Duration(milliseconds: 300));
          _refreshController.loadComplete();
        },
        onRefresh: () async {
          controller.refreshPage();
          _refreshController.refreshCompleted();
        },
        child: Container(
          constraints: const BoxConstraints(
            minHeight: double.infinity,
            minWidth: double.infinity,
          ),
          child: Column(
              children: [
                controller.isShowPasswordDialog
                    ? style.userPasswordDialog(
                        (){
                          // nav to setting password page
                        }, controller.closePasswordDialog)
                    : const SizedBox(),
                ...chatViews.splitJoin(
                  style.divider(context: context, horizontalGap: 6),
                  condition: true
              )] 
          ),
        ),
      ),
    );
  }

  Widget messageRowItem(SessionMessageModel sessionMessage,
      {double avatarGap = 12, Color? bgColor, EdgeInsets? edgeInsets}) {
    return GestureDetector(
      onLongPress: () {},
      child: Container(
        constraints: BoxConstraints(maxHeight: style.messageItemHeight),
        padding: edgeInsets,
        decoration: BoxDecoration(color: bgColor),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            style.userAvatar(sessionMessage.avatar),
            SizedBox(width: avatarGap,),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  style.previewSessionMessage(sessionMessage),
                  style.userInfoRow(sessionMessage),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

}